home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Games of Daze
/
Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso
/
x2ftp
/
msdos
/
libs
/
knowhow4
/
graphics.txt
< prev
next >
Wrap
Text File
|
1994-10-22
|
16KB
|
351 lines
/*
█████████████████████████████████████████████████████████████████████████████
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ KNOW-HOW.GRAPHICS ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
█████████████████████████████████████████████████████████████████████████████
This text contain complete description of KNOW_HOW.GRAPHICS 5.0x, the
product in the 5th version of library. It provide the following features:
a) Using common code for any graphics library. It is realized now for
BGI and for Windows GDI. It is not difficult to add few lines of
code to use any other library, if it support basical operations
(lineto, moveto etc.)
b) Scrolling, Zooming, mirror reflection of image, rotations and even
complex rotations of image. Saying "complex rotations" I mean drawing
of the picture, rotated around x,y to alpha, then (result) around
x1,y1 to beta and so on. BGI fonts could be rotated too, filled BGI
fonts are also available (not all BGI font could be filled, of course).
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Additional (and very powerfull) features are available if KNOW-HOW.GRAPHICS
is used together with KNOW-HOW.SLANG. SLANG is BASIC-like language, designed
as CPP class. Its child classes could have additional operators, so
KNOW-HOW.SLANG.GRAPHICS is the BASIC with access to drawing tools of current
product. It provide to user the possibility to load graphical resourses
(DOS or Windows, or other if he add some code) and interprete them in
run-time. There are very many applications of this tool, like run-time
changeable GUI, maketing (without stage of compilation) of BGI, GDI and so on
applications, vector drawing tools (KNOW-HOW.VECTOR is the ready-to-use
product of this type). Together with BASIC math. functions it could also be
used for data plotting, spreadsheets and so on.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ SERVICE FILES ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
▐▐▐▐▐▐▐ File SIMPLE.H contains some simplifications for often used names.
Complete listing:
▐ #ifndef __SIMPLE_H_
▐ #define __SIMPLE_H_
▐
▐ typedef unsigned char uchar;
▐ typedef unsigned long ulong;
▐ typedef int bool;
▐
▐ #define TRUE 1
▐ #define FALSE 0
▐ #define ON TRUE
▐ #define OFF FALSE
▐
▐ #endif __SIMPLE_H_
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
-----------------------------------------------------------------------------
▐▐▐▐▐▐▐ File GEOM.H contains geometry of KNOW-HOW. It defines geom. objects
in C++ manner: loc(x, y) defines point, rect(x1, y1, x2, y2) - rectangle and
so on.
For example, instead of call to f(int x, int y) we could call f(loc l), where
loc is defined in GEOM.H as
▐ struct loc
▐ { int X,Y;
▐ .....
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
-----------------------------------------------------------------------------
▐▐▐▐▐▐▐ Files TRIGON.H and TRIGON.CPP contains structure with the overloaded
sin(int) and cos(int) functions. This functions works more quick than standart
versions but are not so flexible.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
-----------------------------------------------------------------------------
▐▐▐▐▐▐▐ Files KH_ERROR.H and KH_ERROR.CPP are the part of KNOW-HOW error
handling system. The only variable is defined: kh_error_code. Functions of
KNOW-HOW or user-defined functions could change it, default value is
KH_SUCCESS:
▐ enum { KH_SUCCESS, KH_UNKNOWN_ERROR, KH_FILE_ERROR, KH_BGI_ERROR,
▐ KH_MEMORY_ERROR, KH_USER_ERROR };
This list could be continued:
▐ enum { KH_NEW_ERROR = KH_USER_ERROR + 1, ... }
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ DRAWING MODULES ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
▐▐▐▐▐▐▐ File ABS_GRAF.H contains empty shablons of functions. We could
derivate classes from it not thinking about concrete graphics library we
shell use. It provide common code for any DOS, Windows and so on libraries,
if them include the same functions. This set is only part of Paint class's
functions set, described in PAINT.H:
▐ struct AbstractGraphics
▐ {
▐ virtual void moveto(int x, int y) {}
▐ virtual void lineto(int x, int y) {}
▐ virtual void fillpoly(int numpoints, int* polypoints) {}
▐ };
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
-----------------------------------------------------------------------------
▐▐▐▐▐▐▐ Files BGI_FONT.H and BGI_FONT.CPP contains class, which incapsulate
BGI fonts (*.chr) description.
1. BGI fonts are not documented. I use existing format.
2. Abstract (non-BGI) graphics is used for moveto, lineto, drawpoly and
fillpoly operations. It should be overloaded in child classes for
concrete graf. libraries like BGI, Windows GDI and so on.
3. BGI header have some fields, which are not absolutely necessary (for
current class). I skip them and remarks are not complete for them.
4. No facilities like "vertical text" and so on is supported.
The reason is: I suppose to use drawing extender which have rotation
possibilities in Paint class.
BGI font format is documented in the remarks to code in these files. So I
give here only the brief explanation. BGI use two types of commands for
drawing - moveto and lineto. To define non-filled fonts I use calls to the
same (virtual) functions. To draw filled fonts I use fillpoly() function.
In this case lineto calls are added to points array, and moveto call stop
the polygon creation and begin next one. To use filled fonts you should
use fonts which are drawn as group of closed polygons. Example - 'O' char:
┌───────┐
│▓▓▓▓▓▓▓│
│▓┌───┐▓│
│▓│ │▓│
│▓└─┬─┘▓│
│▓▓▓│▓▓▓│
└───┴───┘
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
-----------------------------------------------------------------------------
▐▐▐▐▐▐▐ Files PAINT.H and PAINT.CPP. In the files described previously, we
have incapsulated graphics primitives like lineto, moveto, drawchar and
so on. Now the additional shell is included for rotation, mirror reflection,
complex rotation, zooming and scrolling. All this operations are performed
with abstract graphics calls, and not depend on library which you intend
to use.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
------------------------- WHAT IS COMPLEX ROTATION ? ------------------------
░Rotation could be simply processed if it is single operation. To use nested
░rotations we use stack of rotations info structures. Example of complex
░rotation:
░
░ void f1(int x, int y, int alpha) { ... perform rotation ... }
░ void f2(int x, int y, int alpha) { rotate(10,10,90); f1(x, y, alpha); }
░Function f2() call rotation procedure and then call f1(), which call another
░rotation. We could a) cancel first rotation and b) add first and second
░rotations to complex transformation. KNOW-HOW.GRAPHICS make possible both
░variances.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
The structure RInfo is used to keep information about single rotation, and
stack of RInfo's is used (if R_STACK variable is set to 1) to keep complex
rotation information.
▐ struct RInfo
▐ {
▐ loc center;
▐ int angle;
▐ };
///////////////
▐ struct Stack
▐ {
▐ int used, total;
▐ RInfo* list;
▐
▐ Stack();
▐ ~Stack();
▐ void push(RInfo* r);
▐ void pop();
▐ void flash();
▐ };
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
-----------------------------------------------------------------------------
Class Paint give some additional facilities for any graphics library. Class
does not contain calls to the concrete drawing functions - only to abstract
prototypes.
functions shold be overloaded in child classes for adaptation to concrete
graphic interface (BGI, GDI, and so on).
The zoom and addzoom are deformation coeficients. You could use zoom in any
part of program. Addzoom is used only once to set the additional deformation
of the whole picture (for example if context is changed from screen to
printer), for preview and so on.
▐ class Paint : public BGI_Font
▐ {
▐ protected:
▐ loc zoom; // Image deformation
▐ loc add_zoom; // Additional deformation
▐ loc lt; // Scroll of part of picture
▐ loc add_scroll; // Left - top clip of the whole picture
▐ int fill; // Fill flag
▐ loc center; // Rotation center
▐ int alpha; // Rotation angle
▐ bool R_STACK; // Use or not stack of rotations
▐ Stack* r_stack; // Stack of rotations
▐ int mirror;
▐ public:
▐ Paint();
▐ ~Paint() { delete r_stack; }
▐
▐ loc get_add_zoom() { return add_zoom; }
▐ loc get_zoom() { return zoom; }
▐
▐ void set_mirror(int m) { mirror = m; } // X coord. if no rotation
▐ void set_stack(bool r) { R_STACK = r; r_stack->flash();
▐ rotate(loc(0, 0), 0); }
▐ void set_zoom(double x, double y) { zoom.X = x * add_zoom.X;
▐ zoom.Y = y * add_zoom.Y; } // No out-of-range control !!!
▐ void set_add_zoom(double x, double y) { add_zoom.X = 100 * x;
▐ add_zoom.Y = 100 * y; }
▐ void set_scroll(int x, int y) { lt.X = x; lt.Y = y; }
▐ void set_add_scroll(int x, int y) { add_scroll.X = x;
▐ add_scroll.Y = y; }
▐ void rotate(loc c, int a);
▐ void set_fill(bool f) { fill = f; }
▐
▐ // Using alpha and center returns rotated coordinates
▐ loc rot(int x, int y);
▐ // Return completely transformed point: rotated, zoomed and scrolled
▐ loc transform(int x, int y);
▐ };
-----------------------------------------------------------------------------
▐▐▐▐▐▐▐ Files BGIPAINT.H and BGIPAINT.CPP. Classes in these files replace
calls to empty graphics primitives functions to concrete calls of functions
from BGI, GDI or from other drawing libraries. The #define directives set
switches on one of libraries, user should unremark one of them to use it
in program:
▐ #define DOS_BGI // Realized
▐ // #define WIN_GDI // Realized
▐ // #define FLASH_GRAPH // Not Realized
▐ // #define OBJECT_GRAPH // Not Realized
▐ // ........ to be continued
The alternative solution is to use Options - Compiler - Code generation -
Defines in IDE to define the necessary variable
As the example, lets look to the code for DOS BGI.
▐ #ifdef DOS_BGI // If we unremark line with DOS_BGI (see above)
▐ #include <graphics.h> // Include GBI header
The following structure do nonthing but replace calls to f() {} to calls
to f() { ::f(); }, where ::f() is BGI function.
▐ struct To_Paint
▐ {
▐ void putpixel(int x, int y) { ::putpixel(x, y, getcolor()); }
▐ void line(int xstart, int ystart, int xend, int yend)
▐ { line(xstart, ystart, xend, yend); }
▐
▐ void moveto(int x, int y) { ::moveto(x, y); }
▐ void lineto(int x, int y) { ::lineto(x, y); }
▐ void fillpoly(int numpoints, int* polypoints)
▐ { ::fillpoly(numpoints, polypoints); }
▐ void circle(int x, int y, int radius) { ::circle(x, y, radius); }
▐ void ellipse(int x, int y, int stangle, int endangle, int xr, int yr)
▐ { ::ellipse(x, y, stangle, endangle, xr, yr); }
▐ void drawpoly(int numpoints, int far* points)
▐ { ::drawpoly(numpoints, points); }
▐ };
▐ #endif DOS_BGI
Then we combine Paint class possibilities to zoom, scroll, rotate (including
complex rotations) and so on and To_Paint possibilities to output graphics
primitives using different libraries.
▐ class KH_Paint : public To_Paint, public Paint
▐ {
▐ public:
▐ KH_Paint() : Paint() {}
▐
▐ void putpixel(int x, int y);
▐ void line(int xstart, int ystart, int xend, int yend);
▐ void lineto(int x, int y);
▐ void moveto(int x, int y);
▐ void circle(int x, int y, int radius);
▐ void ellipse(int x, int y, int stangle, int endangle, int xr, int yr);
▐ void roundrect(int left, int top, int right, int bottom, int radius);
▐
▐ void rectangle(int left, int top, int right, int bottom);
▐ void drawpoly(int numpoints, int far* points);
▐ void outtext(char far* str);
▐ };
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ATTENTION !!! ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
1. The set of functions of KH_Paint class will be continued, it is
possible that your version contains additional functions (setcolor() is the
first candidate).
2. When the Windows compatible part (class To_Paint for Windows) was
designed, I find some difficulties: Windows GDI is more powerfull than DOS
BGI. It is also possible that when user will try to use his graphics library
he find that it have some functions which are absent in both BGI and GDI.
The solution is: use KH_Paint functions if possible, I think that it is much
more than enought for most cases. Another way is to add new functions to
KH_Paint (KH_Paint::draw_pig() {...} and so on, and modify To_Paint for DOS
and Windows), If it is not possible - use direct calls to your lovely library
- and forget about the compatibility with others.
3. KH_Paint above is restricted. The real listing of 5.0 version (and of
course of 5.x) is Windows GDI - oriented. It means that if BGI does not
contain some of GDI features, DOS BGI version of To_Paint class try to
elulate them.
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«
-----------------------------------------------------------------------------
Example.
I add to *.CPP files (when possible) working examples of main() functions,
to run it you need only to remove remarks. In this file I illustrate only
the usage of KNOW-HOW.GRAPHICS with DOS BGI, but BGIPAINT file contains
also example of Windows OWL usage.
▐ void main()
▐ {
▐ int gdriver = DETECT, gmode; // Standart BGI initialization
▐ initgraph(&gdriver, &gmode, ""); // see Borland help system
▐
▐ KH_Paint* paint = new KH_Paint(); // Create new KH_Paint class
▐ paint->load("euro.chr"); // Load BGI font
▐ setcolor(LIGHTRED);
▐ setfillstyle(SOLID_FILL, LIGHTGRAY);
▐ paint->set_fill(ON); // This flag acts on drawpoly
▐ // but not to fonts filling.
▐ paint->set_zoom(0.5, 0.8); // Set picture deformation
▐
▐ for(int i = 0; i < 360; i += 15) // BGI fonts output with rotation
▐ {
▐ paint->rotate(loc(400, 200), i);
▐ paint->outtextxy(10, 200, "Hello, World !");
▐ }
▐
▐ cleardevice();
▐
▐ for(int i1 = 0; i1 < 360; i1 += 15) // Primitives output with rotation
▐ {
▐ paint->rotate(loc(400, 200), i1);
▐ paint->roundrect(300, 100, 400, 200, 20);
▐ int poly[10] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100};
▐ paint->drawpoly(5, poly);
▐ }
▐
▐ cleardevice();
▐ paint->set_stack(ON); // Switch complex rotations ON
▐ for(i = 0; i < 360; i += 30) // First call rotate image to 30,
▐ { // second to 60 and so on.
▐ paint->rotate(loc(400, 200), 30);
▐ paint->outtextxy(10, 200, "Hello, World !");
▐ }
▐
▐ paint->set_stack(OFF);
▐ for(int j = 0; j < 360; j += 15)
▐ {
▐ paint->rotate(loc(400, 200), j); // Ellipse rotations
▐ paint->ellipse(200, 200, 0, 360, 30, 20);
▐ paint->ellipse(300, 200, 50, 120, 30, 20);
▐ }
▐
▐ delete paint;
▐ closegraph();
▐ }